package com.android.happymall.fragment.member;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.happymall.R;
import com.android.happymall.activity.BaseObject;
import com.android.happymall.fragment.BaseLazyFragment;
import com.android.happymall.utils.ConstantsUrl;
import com.android.happymall.utils.ObjectUtils;
import com.android.happymall.utils.update.UpVersionDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/9/7.
 */

public class Fragment_Setting extends BaseLazyFragment {


    @BindView(R.id.setting_feedback)
    TextView setting_feedback;
    @BindView(R.id.setting_cache)
    TextView setting_cache;
    private FragmentActivity mcontext;
    private View view;
    private LayoutInflater inflater;


    private boolean networkConnected;




    public static Fragment_Setting newInstance() {
        Fragment_Setting fragment = new Fragment_Setting();
        Bundle args = new Bundle();

        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {

        }
    }

    @Override
    protected void initPrepare() {
        initUI();
    }

    @Override
    protected void onInvisible() {

    }


    @Override
    protected void initData() {


    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.inflater = inflater;
        view = inflater.inflate(R.layout.fragment_setting, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        return view;
    }
    private Handler mHandler = new Handler() {

        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    ObjectUtils.toast(mcontext, "清除完成");
                    setting_cache.setText(0+"");
                    break;

                default:
                    break;
            }
        };
    };

    private void initUI() {
        try {

        String cache = ObjectUtils.CacheDataManager.getTotalCacheSize(mcontext).toString();
            setting_cache.setText(cache);
        } catch (Exception e) {

            e.printStackTrace();

        }

        String versionCode = ObjectUtils.getVersionName(mcontext);
      //  aboutusVersion.setText("当前版本" + versionCode);

    }


    @OnClick({R.id.setting_feedback,R.id.setting_update,R.id.setting_clear_ll,R.id.setting_servicephone,R.id.mine_exit,R.id.setting_aboutus})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.setting_servicephone:
                Intent callIntent = new Intent(Intent.ACTION_DIAL);
                callIntent.setData(Uri.parse("tel:"+getString(R.string.app_phone)));
                startActivity(callIntent);
                break;
            case R.id.setting_feedback:
                Intent intent = new Intent(getActivity(), BaseObject.class);
                intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_TITLE,"意见反馈");
                startActivity(intent);
                break;
            case R.id.setting_aboutus:
                Intent intent1 = new Intent(getActivity(), BaseObject.class);
                intent1.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_TITLE,"关于我们");
                startActivity(intent1);
                break;
            case R.id.setting_update:
                UpVersionDialog upVersionDialog = new UpVersionDialog();
                upVersionDialog.show(mcontext.getFragmentManager(), "upVersionDialog");
                break;
            case R.id.setting_clear_ll:
                try {
                    ObjectUtils.CacheDataManager.clearAllCache(mcontext);
                    ObjectUtils.toast(mcontext, "清除中");
                    Message msg = new Message();
                    msg.what = 1;
                    mHandler.sendMessage(msg);

//                    if (ObjectUtils.CacheDataManager.getTotalCacheSize(mcontext).startsWith("0")) {
//
//
//                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.mine_exit:
                ObjectUtils.reLogin(mcontext);
                break;
        }
    }


}

